package year2021.month08910;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CollectionTest {
    public static void main(String[] args){
/*        String[] arr = {"hollis ", "Hollis", "HollisChuang"};
//        List<String> list1 = Arrays.asList("hollis ", "Hollis", "HollisChuang");
        List<String> list1 = Arrays.asList(arr);

        for (String o : list1){
            System.out.println(o);
        }

        List<String> list2 = Stream.of("hollis", "Hollis", "HollisChuang").collect(Collectors.toList());
        list2.add("123");

        for (String o : list2){
            System.out.println(o);
        }*/

        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 10));
        personList.add(new Person("张四", 18));
        personList.add(new Person("张五", 13));
        personList.add(new Person("张六", 12));
        personList.add(new Person("张七", 10));
        personList.add(new Person("张八", 13));

        Map<String, Person> personMap1 = personList.stream().collect(Collectors.toMap(Person::getName, Function.identity()));
        Map<Integer, List<Person>> personMap2 = personList.stream().collect(Collectors.groupingBy(Person::getAge));
        Map<Integer, List<Person>> personMap3 = personList.stream().collect(Collectors.toMap(
                Person::getAge,
                o -> new ArrayList<Person>(){{add(o);}},
                (o1, o2) -> {
                    o1.addAll(o2);
                    return o1;
                }
        ));

//        for (String name : personMap1.keySet()){
//            System.out.println(name.concat("-").concat(personMap1.get(name).getAge().toString()));
//        }
        for (Integer age : personMap2.keySet()){
            List<Person> list = personMap2.get(age);
            for (Person o : list){
                System.out.print(o.getName().concat("-").concat(o.getAge().toString()).concat("\t"));
            }
            System.out.println();
        }
        System.out.println();
        for (Integer age : personMap3.keySet()){
            List<Person> list = personMap2.get(age);
            for (Person o : list){
                System.out.print(o.getName().concat("-").concat(o.getAge().toString()).concat("\t"));
            }
            System.out.println();
        }
    }
}


class Person{
    private String name;
    private Integer age;

    public Person(){}
    public Person(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
